<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
    <!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
    <meta name="renderer" content="webkit">
    <!--国产浏览器高速模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>作业管理系统</title>

    <!-- 公共样式 开始 -->
    <link rel="stylesheet" type="text/css" href="../../css/base.css">
    <link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
    <script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
    <script type="text/javascript" src="../../layui/layui.js"></script>
    <!-- 滚动条插件 -->
    <link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
    <script src="../../framework/jquery-ui-1.10.4.min.js"></script>
    <script src="../../framework/jquery.mousewheel.min.js"></script>
    <script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
    <script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
    <!-- 公共样式 结束 -->
    <script type="text/javascript" src="../../framework/frameAll.js"></script>
</head>

<body>
<div class="site-demo-button" id="layerDemo" style="margin-bottom: 0;">
    <button id="addHw" class="layui-btn layui-btn-normal">发布作业</button>
</div>
<fieldset class="layui-elem-field layui-field-title site-demo-button" style="margin-top: 30px;">
    <legend>已发布作业</legend>
</fieldset>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>


<script src="../../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    layui.use('table', function () {
        var table = layui.table;
        var tableIns = table.render({
            elem: '#test',
            height: "full-200",
            method: "get",
            request: {
                pageName: 'pageNo',//页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            where: {
                action: "myPage",
            },
            url: '/HwServlet.do',//数据接口
            page: true, //开启分页
            limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
            cellMinWidth: 100,
            cols: [[
                {title: '序号', type: 'numbers'},
                {field: 'title', title: '标题'},
                {field: 'teacherName', title: '老师姓名'},
                {field: 'describe', title: '作业内容'},
                {field: 'endTime', title: '截止日期'},
                {title: '操作', align: 'center', toolbar: '#barDemo', fixed: 'right'}
            ]]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            //console.log(obj)
            if (layEvent === 'del') {
                layer.confirm('真的删除行么' + data.title, {
                    offset: '100px',
                    icon: 3
                }, function (index) {
                    $.ajax({
                        type: "get",
                        url: "/HwServlet.do",
                        data: {
                            action: "del",
                            id: data.id
                        },
                        dataType: "text",
                        success: function (text) {
                            var tip = parseInt(text);
                            if (tip == 0) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                yesMsg('删除成功!');
                            } else {
                                noMsg('删除失败!');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $("#addHw").click(function () {
            layer.open({
                type: 2,
                title: '发布作业',
                area: ['520px', '80%'],
                resize: false,
                offset: '10px',
                btn: ["提交", "取消"],
                btnAlign: 'c',
                scrollbar: false,
                content: 'upfile.html',//iframe的url
                yes: function (index, layero) {
                    //父页面调用子页面的函数获取返回值
                    var info = $(layero).find("iframe")[0].contentWindow.getInfo();
                    info['action'] = 'add';
                    console.log(info);
                    if (!info['title']) {
                        noMsg("标题不得为空！");
                    }else if (!info['endDate']) {
                        noMsg("截止时间不得为空！");
                    }else {
                        //向后端发送更新信息的请求
                        $.ajax({
                            type: "post",
                            url: "/HwServlet.do",
                            data: info,
                            dataType: "Json",
                            success: function (data) {
                                if (data.tip == 0) {
                                    yesMsg(data.msg);
                                    layer.close(index);
                                    tableIns.reload({
                                        page: {curr: $(".layui-laypage-skip .layui-input").val()}
                                    });
                                } else if (data.tip == 2) {
                                    noMsg(data.msg);
                                    layer.close(index);
                                    tableIns.reload({
                                        page: {curr: $(".layui-laypage-skip .layui-input").val()}
                                    });
                                } else {
                                    noMsg(data.msg);
                                }
                            }
                        });
                    }
                }
            });
        });
    });
</script>

</body>
</html>
